import gameManager from "./gameManager";
import { util } from "./util";

const {ccclass, property} = cc._decorator;

@ccclass
export default class uiManager extends cc.Component {

    private game:gameManager = null; 
    private lastPillarPos:cc.Vec2 = null;           //上一块柱子的位置
    private isCreatePillarState:boolean = false;    //是否允许创建柱子
    private isFirstTouch:boolean = false;           //是否第一次点击
    private isTouchState:boolean = false;           //是否开始点击
    public isEndAction:boolean = true;              //是否结束跳跃动作
    public isMoveState:boolean = false;             //是否处于移动状态（忍者、柱子）
    private isNinjaDown:boolean = false;            //是否忍者处于下降状态
    public time:number = 0;                         //花费的时间
    private timeBackup:number = 0;                  //花费时间备份
    public ninjaTween:cc.Tween = null;              //忍者跳跃动作

    @property(cc.Node)
    ninja:cc.Node = null;
    @property(cc.NodePool)
    pillarPool:cc.NodePool = null;
    @property(cc.Prefab)
    pillarPre:cc.Prefab = null;
    @property(cc.Node)
    tip:cc.Node = null;
    @property(cc.ProgressBar)
    progressBar:cc.ProgressBar = null;
    @property(cc.Label)
    score:cc.Label = null;
    @property(cc.Node)
    uiWrapper:cc.Node = null;

    init(game:gameManager){
        this.game = game
    }
   
    //初始化界面
    initScene(){
        this.lastPillarPos = util.defaultPos
        this.isCreatePillarState = true
        let node = cc.instantiate(this.pillarPre)
        node.x = -450
        node.y = -500
        node.parent = this.uiWrapper
        this.initTween(this.ninja)
        this.initTween(node)
        node.getComponent('pillar').init(this)
        this.ninja.getComponent('ninja').init(this)
        this.initPillarPool()
        this.node.on('touchstart',this.touchStart,this)
        this.node.on('touchend',this.touchEnd,this)
        this.node.on('touchcanel',this.touchEnd,this)
    }

    //点击
    private touchStart(){
        if(!this.isFirstTouch){
            this.tip.active = false
            return
        }
        if(!this.isEndAction)return
        this.isCreatePillarState = true
        this.isTouchState = true
    }

    // 结束点击
    private touchEnd(){
        if(!this.isFirstTouch){
            this.isFirstTouch = true
            return;
        }
        if(!this.isEndAction)return
        this.isEndAction = false
        this.time = this.timeBackup = this.progressBar.progress
        this.isCreatePillarState = false
        this.isTouchState = false
        this.progressBar.progress = 0
        this.changeNinjaOutward()
        this.isMoveState = true
    }

    // 更改忍者外观
    private changeNinjaOutward(){
        cc.resources.load('6404',cc.SpriteFrame,(err,res)=>{
            this.ninja.getComponent(cc.Sprite).spriteFrame = res
        })
    }

    //加分
    public addScore(){
        this.score.string = +this.score.string + 1 + ''
    }

    // 初始化进场动作
    private initTween(node:cc.Node){
        cc.tween(node).by(0.5,{x:200}).start()
    }

    // 初始化柱子节点池
    private initPillarPool(){
        this.pillarPool = new cc.NodePool()
        for(let i = 0; i < 10; i++){
            let node = cc.instantiate(this.pillarPre)
            this.pillarPool.put(node)
        }
        this.createPillar()
    }

    // 创建柱子
    private createPillar(){
        if(this.lastPillarPos.x > cc.winSize.width/2){
            this.isCreatePillarState = false
            return
        }
        let node:cc.Node = null;
        if(this.pillarPool.size()>0){
            node = this.pillarPool.get()
        }else{
            node = cc.instantiate(this.pillarPre)
        }
        node.y = Math.random()*(util.maxY - util.minY) + util.minY
        node.x = this.lastPillarPos.x + Math.random()*(util.maxX - util.minX) + util.minX
        node.parent = this.uiWrapper
        node.getComponent('pillar').init(this)
        this.lastPillarPos = new cc.Vec2(node.x,node.y)
    }

    // 设置忍者下降状态
    private setNinjaDown(){
          cc.resources.load('6405',cc.SpriteFrame,(err,res)=>{
            this.ninja.getComponent(cc.Sprite).spriteFrame = res
        })
        this.ninja.angle = -135
        this.ninjaTween =  cc.tween(this.ninja).to(this.timeBackup*3.5,{y:-cc.winSize.height/2}).call(()=>{
            this.isEndAction = true
            cc.sys.localStorage.setItem('curScore',this.score.string)
            let bestScore = cc.sys.localStorage.getItem('bestScore')
            if(!bestScore){
                cc.sys.localStorage.setItem('bestScore',this.score.string)
            }else{
                if(+this.score.string > +bestScore){
                cc.sys.localStorage.setItem('bestScore',this.score.string)

                }
            }
            this.uiWrapper.removeAllChildren()
            this.game.failGame()
        }).start()
    }
            
    update (dt) {
        if(this.isCreatePillarState){
            this.createPillar()
        }
        if(this.isTouchState){
            this.progressBar.progress += dt * 0.5
            if(this.progressBar.progress >= 1){
                this.progressBar.progress = 1
            }
        }
        if(this.isMoveState){
            this.time -= dt/5
            this.lastPillarPos.x -= dt * util.moveSpeed
            this.createPillar()
            this.ninja.x += dt*30
            if(this.time > this.timeBackup/2){
                this.ninja.y += dt*600
            }
            else if(this.time <= this.timeBackup/2 && this.time > 0){
               if(this.isNinjaDown)return
               this.isNinjaDown = true
               this.setNinjaDown()
            }
            else{
                this.isMoveState = false
                this.timeBackup = this.time = 0
                this.isNinjaDown = false;
            }
        }
    }
}
